package com.ai.service.made.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.made.Cameras;
import com.ai.entity.made.QbeeTemplate;
import com.ai.entity.made.QbeeTemplateConfiguration;
import com.ai.entity.made.QbeeTemplateConfigurationVo;
import com.ai.entity.made.camera.CameraPO;
import com.ai.entity.made.page.QbeeTemplatePage;
import com.ai.mapper.made.QbeeTemplateMapper;
import com.ai.model.param.QbeeTemplateParam;
import com.ai.service.made.IQbeeTemplateConfigurationService;
import com.ai.service.made.IQbeeTemplateService;
import com.ai.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-07-29
 */
@Service
public class QbeeTemplateServiceImpl extends ServiceImpl<QbeeTemplateMapper, QbeeTemplate> implements IQbeeTemplateService {


    @Resource
    private IQbeeTemplateConfigurationService iQbeeTemplateConfigurationService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResponse addNewTemplate(QbeeTemplateParam qbeeTemplateParam) {

        //新增告警模板
        QbeeTemplate qbeeTemplate = new QbeeTemplate();
        qbeeTemplate.setTemplateName(qbeeTemplateParam.getTemplateName());
        qbeeTemplate.setTemplateId(qbeeTemplateParam.getTemplateId());
        qbeeTemplate.setCreateUser(SecurityUtils.getLoginUserName());
        qbeeTemplate.setUpdateUser(SecurityUtils.getLoginUserName());
        qbeeTemplate.setCreateTime(LocalDateTime.now());
        qbeeTemplate.setUpdateTime(LocalDateTime.now());
        this.save(qbeeTemplate);

        if (!CollectionUtils.isEmpty(qbeeTemplateParam.getCameraIdList())){
            iQbeeTemplateConfigurationService.saveConfiguration(qbeeTemplate,qbeeTemplateParam);
        }

        return CommonResponse.ok("模板配置成功");

    }

    @Override
    public CommonResponse getOneTemplateInfo(String id) {

        QbeeTemplate qbeeTemplate = getById(id);
        List<CameraPO> cameraPOList = iQbeeTemplateConfigurationService.getCamerListByTemplateId(id);

        List<String> cameraNameList
                = cameraPOList.stream().map(CameraPO::getNickname).collect(Collectors.toList());

        QbeeTemplateConfigurationVo qbeeTemplateConfigurationVo = new QbeeTemplateConfigurationVo();
        qbeeTemplateConfigurationVo.setId(id);
        qbeeTemplateConfigurationVo.setQbeeTemplateId(qbeeTemplate.getTemplateId());
        qbeeTemplateConfigurationVo.setTemplateName(qbeeTemplate.getTemplateName());
        qbeeTemplateConfigurationVo.setCameraName(cameraNameList);


        return CommonResponse.ok(qbeeTemplateConfigurationVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResponse updateTemplateConfig(QbeeTemplateParam qbeeTemplateParam) {
        //更新模板信息
        UpdateWrapper<QbeeTemplate> qbeeTemplateUpdateWrapper = new UpdateWrapper<>();
        qbeeTemplateUpdateWrapper.eq("id",qbeeTemplateParam.getId());
        qbeeTemplateUpdateWrapper.set("template_name",qbeeTemplateParam.getTemplateName());
        qbeeTemplateUpdateWrapper.set("template_id",qbeeTemplateParam.getTemplateId());

        baseMapper.update(null,qbeeTemplateUpdateWrapper);

        //更新摄像头配置信息
        iQbeeTemplateConfigurationService.updateCameraConfigrationInfo(qbeeTemplateParam);

        return CommonResponse.ok("更新成功");
    }

    @Override
    public CommonResponse delTemplateConfig(String id) {

        //首先删除配置信息表中的数据
        iQbeeTemplateConfigurationService.remove(new QueryWrapper<QbeeTemplateConfiguration>().eq("qbee_template_id",id));

        //删除模板信息
        this.removeById(id);

        return CommonResponse.ok("模板删除成功");
    }

    @Override
    public CommonResponse getCurrBindCamera(String id) {

        List<QbeeTemplateConfiguration> templateConfigurations
                = iQbeeTemplateConfigurationService.list(new QueryWrapper<QbeeTemplateConfiguration>().eq("qbee_template_id", id));

        List<Integer> cameraIds = templateConfigurations.stream().map(qbeeTemplateConfiguration -> {
            Integer cameraId = Integer.parseInt(qbeeTemplateConfiguration.getCameraId());
            return cameraId;
        }).collect(Collectors.toList());

        return CommonResponse.ok(cameraIds);
    }


}
